(function(){
    function GetMachineBalance(){
        this.init();
  
     
       
    }

    GetMachineBalance.prototype = {
        constructor:GetMachineBalance,
        init:function(){
            this.nodata = '<tr><td  colspan="6">暂无信息</td></tr>';
            var _this = this;
            $.datetimepicker.setLocale('ch');
            $('#endtime').datetimepicker({
                format: 'Y-m-d',
                value:new Date().dateadd("d", 1),
                maxDate: new Date(),
                onChangeDateTime:function(dp,$input){
                    var limitStart = new Date($('#begintime').val().replace('-','/'));
                    var nowStart = new Date($('#endtime').val().replace('-','/'));
                    if(limitStart > nowStart){
                        $('#endtime').val(limitStart.format("YY-M-D"))

                    }
                },
                timepicker: false
            });
            
            $('#begintime').datetimepicker({
                format: 'Y-m-d',
                value:new Date().dateadd("d", -1),
                maxDate: new Date(),
                onChangeDateTime:function(dp,$input){
                    var limitStart = new Date($('#endtime').val().replace('-','/'));
                    var nowStart = new Date($('#begintime').val().replace('-','/'));
                    if(limitStart < nowStart){
                        $('#begintime').val(limitStart.format("YY-M-D"))

                    }
                    
                },
                timepicker: false
            });
            
            this.getBalanceList(1);

            $("#searchBtn").unbind("click").click(function(){
                _this.getBalanceList(1);
           });
           $.extend({page:function(pn){
            _this.getBalanceList(pn);
          }});


        
        },
        getBalanceList:function( pn ){
            var _this = this;
            var mid = location.search.getParam("mid");
            var type = $("#flowtype").val() ? "&type="+$("#flowtype").val() : "";
            var stime = "&stime="+$("#begintime").val() ;
            var etime = "&etime="+$("#endtime").val() ;

            var stime1 = $("#begintime").val() ;
            var etime1 = $("#endtime").val() ;
            var desc = $("#remarkbox").val() ? "&desc="+$("#remarkbox").val() : "";
            var cdesc = $("#remarkbox").val()? "&cdesc="+$("#remarkbox").val() :"";

              // 导出明细
              var downLink = '/sys?fid=312&siteid='+mid+type+cdesc+'&stime='+stime1+' 00:00:00&etime='+etime1+' 23:59:59';
              $("#downDetails").attr("href", downLink);
            $.ajax({
                type:"get",
                url:"/sys?fid=309&siteid="+mid+type+desc+stime+etime+"&ps=15&pn="+pn,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        if( data.length )
                        {
                            var strArr = [];
                            var count = info.count;
                            var typeArr = ["扣款","出票","兑奖","充值"];
                            data.forEach(function( value, key  ){
                                var siteid = value.siteid;
                                var sysmoney = parseFloat(value.sysmoney).toFixed(2);//变化金额
                                var wmoney = parseFloat(value.wmoney).toFixed(2);//变化后金额
                                var type = value.type;
                                var ctime = value.createtime;//添加时间
                                var desc = value.ticketid ;//备注

   
                                var html = '<tr><td>'+siteid+'</td><td>'+sysmoney+'</td>';
                                html += '<td>'+wmoney+'</td><td>'+typeArr[type]+'</td><td>'+ctime+'</td><td>'+desc+'</td>';

                                // html += '<td   class="matchineoprate"><a  href="javascript:;"  class="machineRecharge"  data-sid="'+siteid+'"  data-money="'+sysmoney+'">机器充值</a><a  href="javascript:;"   class="machineReduce"   data-sid="'+siteid+'"  data-money="'+sysmoney+'">机器扣款</a><a   href="mflow.html?sid='+siteid+'"  target="_blank">流水明细</a></td>
                                
                                html += '</tr>';
                                strArr.push(html);

                                

                            });

                            $("#machineBalance").html( strArr.join(""));

                            _this.beforeOprate();

                           
                            //  机器加款
                            $("#rechargeBtn").unbind("click").click(function(){
                                _this.doMachineRecharge();
                            });

                            // 机器扣款

                            $("#reduceBtn").unbind("click").click(function(){
                                _this.doMachineReduce();
                            });

                               // 分页
                               var pn = count.pn;
                               var tp = count.tp;
                               var tr = count.tr;
                               $("#countpages").html( tp );
                               $("#countnums").html( tr );
           
                               var pageHtml = getPages(pn,tp, "$.page");
   
                               $("#paginationSel").html(pageHtml);


                           
                          
                        }
                        else
                        {
                            $("#machineBalance").html(_this.nodata);

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }


                }
            });


        },
        // 充值扣款前操作
        beforeOprate:function(){
             // 机器充值
             $(".machineRecharge").unbind("click").click(function(){
                var sid = $(this).data("sid");
                var money = parseFloat( $(this).data("money") );
                $("#reSiteId").val(sid);
                $("#machineSite").html(sid);
                $("#initCount").html(money);
                $("#rechargeCount").html(money);
                $("#rechargeInput").val("");
                $("#machineRechargePanel").modal();
                $("#rechargeInput").on("input", function(){
                    this.value = this.value.replace(/[^\d.]/,"");
                    this.value = this.value.replace(/^\./g,"");
                    this.value = this.value.replace(".","$#$").replace(/\./g, "").replace("$#$",".");
                    var rMoney = parseFloat(this.value ) ;
                    rMoney = rMoney ? rMoney : 0;
                    var rMoney1 = (rMoney + money).toFixed(2);
                    $("#rechargeCount").html(rMoney1);
                });
            });

             // 机器扣款
             $(".machineReduce").unbind("click").click(function(){
                var sid = $(this).data("sid");
                var money = parseFloat( $(this).data("money") );
                $("#reSiteId1").val(sid);
                $("#machineSite1").html(sid);
                $("#initCount1").html(money);
                $("#reduceCount").html(money);
                $("#reduceInput").val("");
                $("#machineReducePanel").modal();
                $("#reduceInput").on("input", function(){
                    this.value = this.value.replace(/[^\d.]/,"");
                    this.value = this.value.replace(/^\./g,"");
                    this.value = this.value.replace(".","$#$").replace(/\./g, "").replace("$#$",".");
                    var rMoney = parseFloat(this.value ) ;
                    rMoney = rMoney ? rMoney : 0;
                    var rMoney1 = ( money - rMoney).toFixed(2);
                    $("#reduceCount").html(rMoney1);
                });
            });


        },
        // 机器充值
        doMachineRecharge:function(){
            var sitId = $("#reSiteId").val();
            var wmoney = parseFloat( $("#rechargeInput").val() ).toFixed(2);
            var reDesc = $("#rechargedesc").val();
            if(wmoney <= 0 )
            {
                $("#rechargeInput").val("").focus();
                return false;
            }

            var sendData = {
                siteid:sitId,
                wmoney:wmoney,
                desc:reDesc
            };

            $.ajax({
                type:"post",
                url:"/sys?fid=310&type=3",
                data:sendData,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $("#rechargeBtn").popover("show");
                        setTimeout(function(){
                            $("#rechargeBtn").popover("hide");
                            $("#machineRechargePanel").modal("hide");

                        }, 1000);

                    }
                    else
                    {
                        $("#machineRechargePanel").modal("hide");
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {
                           
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }
                    }

                }
            });
            




        },
          // 机器扣款
          doMachineReduce:function(){
            var sitId = $("#reSiteId1").val();
            var wmoney = parseFloat( $("#reduceInput").val() ).toFixed(2);
            var reDesc = $("#reducedesc").val();
            if(wmoney <= 0 )
            {
                $("#reduceInput").val("").focus();
                return false;
            }

            var sendData = {
                siteid:sitId,
                wmoney:wmoney,
                desc:reDesc
            };

            $.ajax({
                type:"post",
                url:"/sys?fid=310&type=0",
                data:sendData,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $("#reduceBtn").popover("show");
                        setTimeout(function(){
                            $("#reduceBtn").popover("hide");
                            $("#machineReducePanel").modal("hide");

                        }, 1000);

                    }
                    else
                    {
                        $("#machineReducePanel").modal("hide");
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {

                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }
                    }

                }
            });
            




        }

 
      
      
    };





  new GetMachineBalance();

}());